package com.darly.book.presenter;

import com.darly.book.model.bean.BookDetailBean;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.BookDetailContract;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.book.utils.MD5Utils;
import com.darly.chinese.db.book.table.BookChapterBean;
import com.darly.chinese.db.book.table.CollBookBean;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.db.version.DataBaseController;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import java.util.ArrayList;

import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BookDetailPresenter extends RxPresenter<BookDetailContract.View>
        implements BookDetailContract.Presenter {
    private static final String TAG = "BookDetailPresenter";
    private String bookId;

    @Override
    public void refreshBookDetail(String bookId) {
        this.bookId = bookId;
        refreshBook();
        refreshComment();
        refreshRecommend();

    }

    @Override
    public void addToBookShelf(CollBookBean collBookBean) {
        Disposable disposable = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookChapterPackage(collBookBean.get_id(), "chapter")
                .map(bean -> {
                    if (bean.getMixToc() == null) {
                        return new ArrayList<BookChapterBean>(1);
                    } else {
                        return bean.getMixToc().getChapters();
                    }
                })
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(
                        (d) -> mView.waitToBookShelf() //等待加载
                )
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        beans -> {

                            //设置 id
                            for (BookChapterBean bean : beans) {
                                bean.setId(MD5Utils.strToMd5By16(bean.getLink()));
                            }

                            //设置目录
                            collBookBean.setBookChapters(beans);
                            //存储收藏
                            DataBaseController.save(collBookBean);

                            mView.succeedToBookShelf();
                        }
                        ,
                        e -> {
                            mView.errorToBookShelf();
                            DLog.e(e);
                        }
                );
        addDisposable(disposable);
    }

    private void refreshBook() {
        RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookDetail(bookId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<BookDetailBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onSuccess(BookDetailBean value) {
                        mView.finishRefresh(value);
                        mView.complete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError();
                    }
                });
    }

    private void refreshComment() {
        Disposable disposable = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getHotCommnentPackage(bookId)
                .map(bean -> bean.getReviews())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (value) -> mView.finishHotComment(value)
                );
        addDisposable(disposable);
    }

    private void refreshRecommend() {
        Disposable disposable = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getRecommendBookListPackage(bookId, 3 + "")
                .map(bean -> bean.getBooklists())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (value) -> mView.finishRecommendBookList(value)
                );
        addDisposable(disposable);
    }
}
